//<script language="Javascript">
<?


//////////////////  ASSETS  //////////////////
if($_set->wtype == "win") {
//////////////////////////////////////////////

////////////////  IMAGE ASSETS
if($_tool[1] == "img") {

////////////////// MANAGE IMAGES
	if($_set->action == "new"){
	
	$dump = $DB->filter( "media.img", "active=0 ORDER BY ID DESC LIMIT ".$_set->vars->flow->limit );
	if($dump) foreach($dump as $d) {
		$meta = json_decode($d['meta']);
		$dump_js[] = "{id:".$d['ID'].", type:'img', tool:'media.img', name:".$d['ID'].", descr:\"".$meta->raw."\", meta:{ objtype:8, img:".$d['ID']." }}";
	}
	
	if($_set->vars->limit && count($dump_js) == (int) $_set->vars->limit ) $total = $DB->query_direct( "select count(*) from db_media where !active" );
	
	?>
	
	ref.seed([
		tag("font", {style:"font-size:18px"}, "Beheer beelden"), br,
		tag("div", {id:"media.dump", style:"margin-top:20px;"})
	]);
	
	win.asset.dropbox.build({id:'media.dump', descr:'image dump', type:'img', new:"add media", tool:'media.img', <? if($total) echo "flow:{total:$total, cur:0, itv:".$_set->vars->limit."}, "; ?>floats:[<?=($dump_js)?implode(",",$dump_js):"";?>]});

	///// IMG BUTTON, CODE SHOULD BE IN DROPBOX CLASS
		Object.extend(loadcluster.init, {
			button_placeholder_id:"media.dump.new.txt",
			post_params : { version:0.1, account :'<?=ACCOUNT?>', auth:<?=$Engine->user('ID')?>, objtype : <?=$DB->type( "media.img" )?>, active : 0 },
			custom_settings : { float_target : 'media.dump', objtype : <?=$DB->type( "media.img" )?> }
		} );
		var swfu = new SWFUpload(loadcluster.init);
		
	<? exit(); }


	if($_set->action == "edit.new"){ ?>
	
		ref.remove( );

	<? exit(); }

}

/*///////////////  IMAGE ASSETS
if($_tool[1] == "flash") {

////////////////// MANAGE IMAGES
	if($_set->action == "edit.mod"){
		
		$Modcon = new MOD_CONSTRUCT();
		$_mod = $_set->vars->meta->modvars;
		$_modset = $Modcon->request_json( $_set->vars->meta->modvars->setid );
		
		$n = 0;
		foreach($_modset->publ as $key => $publ) {
			if($n==2) { $n=0; $table[] = "]), tag('tr',0,["; }
			$table[] = "tag('td',0,[ tag('input',{id:ref.id+'.$key', style:'width:92px;'}) ]),";
			$objs[] = "{id:ref.id+'.$key', descr:'$key', tool:'media.flash.$key', type:'mod', meta: {value:'$publ', field:'json.publ.$key', objid:".$_set->vars->meta->modvars->setid."}}";
		$n++; }
		if (count($objs) % 2) $table[] = "tag('td',0,' '),";

	?>	
		ref.seed([
			tag("font", {style:"font-size:18px"}, "Modify flash object "), tag("a", {id:ref.id+".h1", href:"http://<?=engine_get('control.public')?>/?page=<?=$_set->vars->meta->page?>", target:"_blank"}, "(<?=$_set->vars->meta->page?>)"), br,
			tag("input", {id:ref.id+".name"}),
			tag("div", {id:ref.id+".extra"}),
			tag("div", {id:ref.id+".comment", class:"comment"})
		]);
		
		groups = { extra : [
			tag("table",{cellpadding:"0", cellspacing:"0"},[
				tag("tr",0,[
					<?= substr(implode("\n", $table), 0, -1); ?>
				])
			])
		]};
		
		win.asset.group.build({id:ref.id+".extra", descr:'Specificaties', cont:groups.extra, open:1 });
		
		[	{id:ref.id+".name", descr:'title', direct:1, traceback:<?= json_encode($_set->vars->traceback) ?>, textid:<?=$_mod->nameid?>, values:<?=($_mod->nameid)?json_encode($DB->parse_by(array("lang","short"),"txtdump","textid=".$_mod->nameid, true)):"{}"?>}
		].each(function(obj){ win.asset.txtfield.build(obj); });
		
		[	<?= implode(",\n",$objs); ?>
		].each(function(obj){ win.asset.ajxfield.build(obj); });

		
		
		
	<? exit(); }

}*/
///////////////// AJAX COMM //////////////////
} else if($_set->wtype == "push") {
//////////////////////////////////////////////


////////////////// CHANGE ORDER
if($_set->action == "hierarchy") {
	// IMAGE
	if($_set->vars->type == "img"){	
		// INCASE NORMAL DRAG OR COPY
		if($_set->vars->addchild) {
			$DB->query("INSERT INTO glue_object (parenttype, parentid, objtype, objid) VALUES (".$_set->vars->objtype.", ".$_set->vars->objid.", ".$_set->vars->childtype.", ".$_set->vars->addchild[0].")");
			//if($_set->vars->addchild) $DB->query("INSERT INTO glue_object (parenttype, parentid, objtype, objid) VALUES (".$_set->vars->typeid.", ".$_set->vars->dbid.", ".$_set->vars->objtypeid.", ".(($_set->vars->addchild)$_set->vars->addchild[0]?:$_set->vars->copychild[0]).")");
			if($_set->vars->childtype) $DB->query("UPDATE db_media SET `type`=".$_set->vars->childtype.", active=1 WHERE ID=".$_set->vars->addchild[0]);
			
		}
									
		//if($_set->vars->id == "sitemap") $_set->vars->id = 0;
		//foreach($_set->vars->floats as $k => $v) $DB->query("UPDATE `pages` SET `hierarchy`=$k, llog=NOW() WHERE `ID`=$v AND parent=".$_set->vars->id);
		
		
		?>
		ref.id = "sitemap.comment";
		xhttp.gfx("hierarchy updated", ref.id);
		
	<? }

exit(); }


/////////// LIST CONTROL

if($_set->action == "list") { ?>

	$(ref.trg).seed(<?= $_set->parse_list() ?>);
	
<? exit(); }

if( strstr( $_set->action, "flow") ) { ?>
	
	$('control_list')._push(ref.trg, <?= $_set->flow() ?> );

<? exit(); }


////////////////// DUMP ELEMENT
if($_set->action == "kill") {
	// MEDIA
	function deleteFileFromDir($path, $ref) {
		$dir = opendir($path); $log = array();
		while (false !== ($file = readdir($dir))) { if(strpos($file, ".")!==0) {
		
			if(is_dir($path.$file)) $log = array_merge($log, deleteFileFromDir("$path$file/", $ref));
			else if(strpos($file, (string)$ref) === 0) {
				$name = explode(".", $file);
				if($name[0] == (string) $ref) {
					unlink($path.$file);
					$log[] = $file;
				}
			}
		}}
		closedir($dir);
		return $log;
	}
			
	if($_set->vars->type == "img" || $_set->vars->type == "media") {
		$img = $DB->get( "media.img", $_set->vars->id );
		if( !(int) $img['active'] ) {
			if( deleteFileFromDir( ROOT . ACCOUNT ."/www/DB/media/", $_set->vars->id) ) {
				$log = "destroyed";
			} else $log = "not processed (warning)";
			
			if( $img )$DB->query("DELETE FROM db_media WHERE (type=8 or active=0) && ID=".$_set->vars->id, 0 );
			
		} else {
			$DB->query("UPDATE db_media SET active=0 WHERE ID=".$_set->vars->id, 0);
			$DB->query("DELETE FROM glue_object WHERE objtype=".$_set->vars->meta->objtype." AND objid=".$_set->vars->id, 0 ); ///////should be ".$_set->vars->meta->objtype."
			// remove from glue!!!
			$log = "moved to dump";
		}
		?>
		dashboard.comment("<?=$_set->vars->type." (" .$_set->vars->id. ") ".$log;?>");
	<? }
exit(); }





////////////////// SHIFT FUNCTIONALITIES
if($_set->action == "shift") {

	$comment = ( $_set->shift() )? "option modified":"warning: failed";

	?>
	xhttp.gfx("<?=$comment?>", ref.id );
	<?
	
exit(); }

////////////////// AJAX (direct) UPDATE FUNCTIONALITIES
if($_set->action == "ajax") {
	
	if(!$_set->vars->meta->field) {
		// customise
	}
	if($_tool[1]=="stream" && !$_set->vars->meta->objid) {
		$DB->query("insert into db_media(type, flog, creator) values (14, now(), ".$_SESSION['uid'].")", 0 );
		$_set->vars->meta->objid = $DB->query_direct( "select max(ID) from db_media" );
		$DB->query("insert into glue_object(parenttype, parentid, objtype, objid, user) values (".$_set->vars->meta->parenttype.", ".$_set->vars->meta->parentid.", 14, ".$_set->vars->meta->objid.", ".$_user['ID'].")", 0 );
		?>
		win.asset.ajxfield.list['<?=$_set->vars->id?>'].meta.objid = <?=$_set->vars->meta->objid?>;
		<?
	}
	
	$comment = ( $_set->ajax() )? "updated element":"warning: failed";
	
	?>
	dashboard.comment("<?=$comment?>");
	<?
	
exit(); }
}
